package mysql

import (
	"bw-renting/config"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var db *gorm.DB

func init() {
	dsn := config.M.Name + ":123456@tcp(127.0.0.1:3306)/bawei_renting?charset=utf8mb4&parseTime=True&loc=Local"
	var err error
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		logx.Error("初始化mysql链接失败")
		return
	}

	err = db.AutoMigrate(new(User), new(Area), new(House), new(Facility), new(HouseFacility), new(HouseOrder))
	if err != nil {
		logx.Error("创建mysql数据表失败")
		return
	}
}

// User 用户表
type User struct {
	gorm.Model
	Mobile       string `gorm:"type:char(11);unique;not null"`
	Password     string `gorm:"type:char(100);not null"`
	Email        string `gorm:"type:varchar(100)"`
	AvatarUrl    string `gorm:"type:varchar(100);"`
	RealName     string `gorm:"type:varchar(100);"`
	IdentityCard string `gorm:"type:char(18);"`
}

// Area 地区表
type Area struct {
	Id   uint64 `gorm:"primarykey"`
	Name string `gorm:"type:varchar(100);not null"`
}

// House 房源表
type House struct {
	gorm.Model
	UserId    int64   `gorm:"type:int;not null"`
	Title     string  `gorm:"type:varchar(100);not null"`
	AreaId    int64   `gorm:"type:int;not null"`
	Price     float64 `gorm:"type:decimal;not null"`
	Address   string  `gorm:"type:varchar(100);not null"`
	RoomCount int64   `gorm:"type:int;not null"`
	Acreage   int64   `gorm:"type:int;not null"`
	Unit      string  `gorm:"type:varchar(100);not null"`
	CoverUrl  string  `gorm:"type:varchar(100);not null"`
}

// Facility 设施表
type Facility struct {
	Id   uint64 `gorm:"primarykey"`
	Name string `gorm:"type:varchar(100);not null"`
}

// HouseFacility 房源设施表
type HouseFacility struct {
	Id      uint64 `gorm:"primarykey"`
	Name    string `gorm:"type:varchar(100);not null"`
	HouseId int64  `gorm:"type:int;not null"`
}

// HouseOrder 租房订单表
type HouseOrder struct {
	gorm.Model
	UserId    int64   `gorm:"type:int;not null"`
	HouseId   int64   `gorm:"type:int;not null"`
	Title     string  `gorm:"type:varchar(100);not null"`
	OrderSn   string  `gorm:"type:varchar(100);not null"`
	StartDate int64   `gorm:"type:int;not null"`
	EndDate   int64   `gorm:"type:int;not null"`
	Price     float64 `gorm:"type:decimal;not null"`
	Status    int64   `gorm:"type:int;not null"`
}
